package com.tianhao.luo.nouse.factory;

import com.tianhao.luo.nouse.tree.Tree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 游戏中地图上生产树的工厂，节省了内存占用，因为它们一旦创建就不会再发生改变。
 * @author tianhao.luo
 * @date 2019/12/13 16:51
 */
public class TreeFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(TreeFactory.class);
    /**
     * 用一个对象存储常用的记录，同一对象无需多次创建
     */
    private static Map<String, Tree> map = new ConcurrentHashMap<>();

    public static Tree getTree(String name,String data) {
        //判断树map中是否已有该树的记录了
        if (!map.containsKey(name)) {
            Tree tree = new Tree(name, data);
            LOGGER.info("{}树创建了",name);
            map.put(name,tree);
            return tree;
        }else {
            return map.get(name);
        }
    }
}
